package com.xxx.demo.client.job.service;

import com.alipay.remoting.exception.RemotingException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.butid.data.RpcNettyClient;
import com.github.butid.data.codec.RequestBody;
import com.github.butid.data.rpc.RpcCallback;
import com.xxx.demo.client.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * JObService示例
 * @author wangjing.dc@qq.com
 * @since 2020/8/7
 */
@Service
@Slf4j
public class DemoJobService {

    @Value("${client.dataServer}")
    private String serverAddr;

    @Autowired
    private RpcNettyClient client;
    @Autowired
    private DemoService demoService;


    /**
     * 发送流水信息
     */
    public void sendTransLog() {
        log.debug("定时发送流水信息");
        // @CommandController(cmdType = "STAT", cmdCode = "trans")
        RequestBody req = new RequestBody()
                .setCmdType("STAT")
                .setCmdCode("trans")
                .addBody("logs", this.demoService.queryTranslog());
        this.send(req);
    }

    private void send(RequestBody req) {
        try {
            this.client.send(serverAddr, req, new RpcCallback(){
                @Override
                public void onResponse(Object result) {
                    super.onResponse(result);
                    log.info("服务端返回结果{}", result);
                }
            });
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
